package com.test.demo;

import java.util.Random;

public class ArrayTest4 {
    public static void main(String[] args) {
        //目标：完成斗地主的洗牌和发牌
        String[] poker = new String[54];
        makePoker(poker);
        System.out.println("洗牌完毕");
        for (int i = 0; i < poker.length; i++) {
            System.out.print(poker[i] + " ");
            if ((i + 1) % 13 == 0) {
                System.out.println();
            }
        }
        rangdomPoker(poker);
        System.out.println("发牌完毕");
        for (int i = 0; i < poker.length; i++) {
            System.out.print(poker[i] + " ");
            if ((i + 1) % 13 == 0) {
                System.out.println();
            }
        }
    }

    // 1.定义一个方法，用来洗牌
    public static String[] makePoker(String[] poker) {//返回值类型是String[]，参数列表是String[] poker
        // 1.1定义一个数组，存储54张牌
        //poker = [null,null,null ...];//默认值为null
        // 1.2定义一个数组，存储花色
        String[] colors = {"♠", "♥", "♣", "♦"};
        // 1.3定义一个数组，存储点数
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        // 1.4定义一个变量，记录牌的索引
        int index = 0;
        // 1.5遍历花色和点数，组装牌
        for (int i = 0; i < numbers.length; i++) {//先遍历点数
            for (int j = 0; j < colors.length; j++) {//再遍历花色
                poker[index] = colors[j] + numbers[i];
                index++;
            }
        }
        // 1.6单独添加大小王
        poker[index++] = "🃏b";
        poker[index] = "🃏s";
        return poker;
    }

    // 2.定义一个方法，用来发牌
    public static String[] rangdomPoker(String[] poker) {
/*      poker = [♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♠J, ♠Q, ♠K, ♠A, ♠2,
                 ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♥J, ♥Q, ♥K, ♥A, ♥2,
                 ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♣J, ♣Q, ♣K, ♣A, ♣2,
                 ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10, ♦J, ♦Q, ♦K, ♦A, ♦2,
                 🃏b, 🃏s];*/
        Random num = new Random();
        for (int i = 0; i < poker.length; i++) {
            int index1 = num.nextInt(poker.length);
            int index2 = num.nextInt(poker.length);
            String temp = poker[index1];//创建一个临时变量，用来交换两个元素的值
            poker[index1] = poker[index2];
            poker[index2] = temp;
        }
        return poker;
    }
}
